﻿using GameFramework;
using UnityEngine;

namespace GFTest
{
    /// <summary>
    /// NGUI 子界面抽象类。
    /// </summary>
    public abstract class NGUISubForm : MonoBehaviour
    {
        [SerializeField]
        private NGUIForm m_OwnerUIForm = null;

        private UIPanel m_CachedUIPanel = null;

        private UIEffectsController m_UIEffectsController = null;

        public NGUIForm OwnerUIForm
        {
            get
            {
                return m_OwnerUIForm;
            }
            set
            {
                m_OwnerUIForm = value;
            }
        }

        public int OriginalDepth
        {
            get;
            private set;
        }

        public int Depth
        {
            get
            {
                return m_CachedUIPanel.depth;
            }
        }

        public void Init(object userData = null)
        {
            OnInit(userData);
        }

        public void Open(object userData = null)
        {
            gameObject.SetActive(true);
            OnOpen(userData);
        }

        public void Close(object userData = null)
        {
            if (!gameObject.activeSelf)
            {
                return;
            }

            OnClose(userData);
            gameObject.SetActive(false);
        }

        public void PlayUISound(int uiSoundId)
        {
            //GameEntry.Sound.PlayUISound(uiSoundId);
        }

        protected virtual void OnInit(object userData)
        {
            m_CachedUIPanel = GetComponent<UIPanel>();
            if (m_CachedUIPanel == null)
            {
                Log.Warning("UIPanel is invalid.");
                return;
            }

            m_UIEffectsController = GetComponent<UIEffectsController>();

            OriginalDepth = m_CachedUIPanel.depth;

            UILabel[] uiLabels = GetComponentsInChildren<UILabel>(true);
            for (int i = 0; i < uiLabels.Length; i++)
            {
                if (!string.IsNullOrEmpty(uiLabels[i].text))
                {
                    uiLabels[i].text = GameEntry.Localization.GetString(uiLabels[i].text);
                }
            }
        }

        protected virtual void OnOpen(object userData)
        {
            if (m_UIEffectsController != null)
            {
                m_UIEffectsController.Show();
            }
        }

        protected virtual void OnClose(object userData)
        {
            if (m_UIEffectsController != null)
            {
                m_UIEffectsController.Pause();
            }
        }
    }
}
